// pages/wdht/wdht.js
const app = getApp();
var common = require('../../utils/common.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fylist: [],
    array: [{ "houseId": "", "name": "全部" }],
    index: 0,
    total: 0,
    page:1,
    houseId:"",
  },
  // bindPickerChange: function (e) {
  //   var that = this;
  //   wx.showLoading({
  //     title: '加载中',
  //   })
  //   this.setData({
  //     index: e.detail.value,
  //   })
  //   wx.request({
  //     url: app.globalData.url + '/wx/user/houselist',
  //     data: {
  //       token: wx.getStorageSync('token'),
  //       status: 0,
  //       houseId: that.data.array[that.data.index].houseId
  //     },
  //     header: {
  //       'content-type': 'application/json' // 默认值
  //     },
  //     success: function (res) {
  //       if (res.data.success == true) {
  //         res.data.data.list[0]["roomId"] = "1";
  //         console.log(res.data.data.list);
  //         that.setData({
  //           fylist: res.data.data.list,
  //           total: res.data.data.total
  //         })
  //       } else {
  //         wx.showToast({
  //           title: res.data.msg,
  //           image: '../../image/shibai.png',
  //           duration: 2000
  //         })
  //       }
  //     },
  //     fail: function (res) {
  //       wx.showToast({
  //         title: '房间列表获取失败',
  //         image: '../../image/shibai.png',
  //         duration: 2000
  //       })
  //     },
  //     complete: res => {
  //       wx.hideLoading()
  //     }
  //   })
  // },
  //选择房源
  bindPickerChange: function (e) {
    var that = this;
    var index = e.detail.value;
    var id = this.data.array[index].houseId;
    this.setData({
      fylist:[],
      index: index,
      page: 1,
      houseId:id,
    })
    console.log(id);
    this.getRoomsList(id);

  },


  getRoomsList: function (houseId) {
    var that = this;
    common._get('/wx/user/houselist', { page: this.data.page, houseId: houseId, status: 0 },
      //成功
      function (res) {
        if (res.data.success) {
          console.log(res.data)
          if (res.data.data.list) {
            that.data.page++;
            var temp = that.data.fylist.concat(res.data.data.list);
            that.setData({
              // fylist:res.data.data.list,
              fylist: temp,
              total: res.data.data.totalCount,
            })
          }
        }
      });
  },

  toDetail: function (e) {
    wx.navigateTo({
      url: '../waitingRentRoom/waitingRentRoom?room_id=' + e.currentTarget.dataset.room_id
    })
  },

  initData: function () {
    var that = this;
    //获取房源分组
    common._get('/wx/house/houselist', { page: 1, houseId: that.data.array[that.data.index].houseId },
      //成功
      function (res) {
        if (res.data.success) {
          console.log(res.data)
          var houseList = res.data.data.list;
          var newArray = that.data.array;
          for (var i in houseList) {
            var houseJson = {};
            houseJson["houseId"] = houseList[i].houseId;
            houseJson["name"] = houseList[i].houseName;
            newArray.push(houseJson);
          }
          that.setData({
            array: newArray
          })
        }
      });
    this.getRoomsList('');
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getRoomsList(this.data.houseId);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})